#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/wait.h>


int main()
{
  pid_t id=fork();
  if(id==0)
  {
    int cnt=5;
    while(cnt)
    {
      printf("我是子进程，仍然存在，还有cnt：%d,pid：%d，ppid%d\n",cnt--,getpid(),getppid());
      sleep(1);
    }
    exit(0);
  }
  while(1)
  {
    int status=0;
    pid_t ret_id=waitpid(id,&status,0);
    if(ret_id<0)
    {
      printf("waitpid error\n");
      exit(1);
    }
    else if(ret_id==0)
    {
      printf("子进程尚未退出，父进程异步其他功能\n");
    }
    else
    {
      printf("我是父进程，等待子进程，pid:%d，ppid%d,ret_id:%d,child exit code:%d,child exit signal:%d\n",getpid(),getppid(),ret_id,(status>>8)&0xFF,status & 0x7F);
    }
  }
}

